package com.dure.hotmemory.scenes;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.dure.hotmemory.R;
import com.dure.hotmemory.adapters.AppAdapter;
import com.dure.hotmemory.bean.Recommend;
import com.dure.hotmemory.config.Constants;
import com.dure.hotmemory.controllers.AppController;
import com.dure.hotmemory.drawview.BaseSurfaceView;
import com.dure.hotmemory.listeners.ConfirmListener;
import com.dure.hotmemory.server.MyService;
import com.umeng.analytics.MobclickAgent;


/**
 * Created by leix on 27/11/2016.
 */

public class AppScene extends BaseScene {
    private String currentSectedUrl, currentSelectedAppName;
    private boolean showRecommend;
//    LinearLayout recommendLayout;

    AppController controller;
    private int downloadIndex = 22;

    FrameLayout backFL;
    private AppAdapter appAdapter;
    private ListView recommendLV;

    public AppScene(BaseSurfaceView baseSurfaceView) {
        super(baseSurfaceView);
        controller = new AppController(this);
        initData();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            backFL.setBackgroundResource(R.drawable.white_planet_drawable);
        }else{
            backFL.setBackgroundResource(R.drawable.white_circle_drawable);
        }
    }

    @Override
    public void initData() {
        sceneView = LayoutInflater.from(context).inflate(R.layout.app_layout, null);
        findViews();
        addListener();
        controller.checkShouldShowRecommend();

    }

    private void findViews() {
        backFL = (FrameLayout) sceneView.findViewById(R.id.back_fl);
//        recommendLayout = (LinearLayout) sceneView.findViewById(R.id.recommend_layout);
        recommendLV = (ListView) sceneView.findViewById(R.id.recommend_lv);
        appAdapter = new AppAdapter(activity);
        recommendLV.setAdapter(appAdapter);
    }

    private void addListener() {
        backFL.setOnClickListener(activity);
        appAdapter.setConfirmListener(new ConfirmListener() {
            @Override
            public void confirmed(Bundle bundle) {
                startDownload(bundle);
            }
        });

    }



    private void startDownload(Bundle bundle) {
        currentSelectedAppName = bundle.getString("app_name");
        currentSectedUrl = bundle.getString("url");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0版本以后的要进行动态授权
            if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                //申请WRITE_EXTERNAL_STORAGE权限
                activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            } else {
                goToDownload();
            }
        } else {
            goToDownload();
        }
    }

//    private void showRecommendApps(ArrayList apps) {
//        recommendLayout.removeAllViews();
//        final String currentPackageName = context.getPackageName();
//        for (int i = 0; i < apps.size(); i++) {
//            LinkedTreeMap appMap = (LinkedTreeMap) apps.get(i);
//            String packageName = (String) appMap.get("package");
//            if (TextUtils.equals(currentPackageName, packageName)) {
//                continue;
//            }
//            final String appName = (String) appMap.get("app_name");
//            String size = (String) appMap.get("size");
//            String desc = (String) appMap.get("desc");
//            final String url = (String) appMap.get("url");
//            String icon = (String) appMap.get("icon");
//
//            View view = LayoutInflater.from(context).inflate(R.layout.recommend_item_layout, null);
//            TextView nameTV = (TextView) view.findViewById(R.id.name_tv);
//            TextView descTV = (TextView) view.findViewById(R.id.desc_tv);
//            TextView sizeTV = (TextView) view.findViewById(R.id.size_tv);
//            ImageView appIV = (ImageView) view.findViewById(R.id.app_iv);
//            ImageView downloadIV = (ImageView) view.findViewById(R.id.download_iv);
//
//            nameTV.setText(appName);
//            descTV.setText(desc);
//            sizeTV.setText(size);
//
//            x.image().bind(appIV, icon);
//
//            downloadIV.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    currentSelectedAppName = appName;
//                    currentSectedUrl = url;
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0版本以后的要进行动态授权
//                        if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                                != PackageManager.PERMISSION_GRANTED) {
//                            //申请WRITE_EXTERNAL_STORAGE权限
//                            activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
//                                    1);
//                        } else {
//                            goToDownload();
//                        }
//                    } else {
//                        goToDownload();
//                    }
//
//
//                }
//            });
//
//            recommendLayout.addView(view);
//        }
//    }

   public void setRecommendData(Recommend recommend){
       if (recommend.status == 1) {
           showRecommend = recommend.show;
       }

       if (showRecommend) {
//            recommendLayout.setVisibility(View.VISIBLE);
//            showRecommendApps(recommend.apps);
           appAdapter.setData(recommend.apps);
       }
   }

//    @Override
//    public void permissionGranted() {
//        goToDownload();
//    }

    private void goToDownload() {
//        Intent intent = new Intent(this, UpdateService.class);
        Intent intent = new Intent(context, MyService.class);
        intent.putExtra("url", currentSectedUrl);
        if ("热视读".equals(currentSelectedAppName)) {
            MobclickAgent.onEvent(context, "fastread_download");
            intent.putExtra("img_id", R.mipmap.ic_launcher);
            intent.putExtra("notify_id", Constants.FASTREAD_DOWNLOAD_INDEX);
        } else if (TextUtils.equals("热呼吸", currentSelectedAppName)){
            MobclickAgent.onEvent(context, "hotbreath_download");
            intent.putExtra("img_id", R.mipmap.ic_launcher);
            intent.putExtra("notify_id", Constants.HOTBREATH_DOWNLOAD_INDEX);
        }else if (TextUtils.equals("热亲", currentSelectedAppName)){
//            MobclickAgent.onEvent(context, "hotbreath_download");
            intent.putExtra("img_id", R.mipmap.ic_launcher);
            intent.putExtra("notify_id", Constants.RELATIONSHIP_DOWNLOAD_INDEX);
        }
        intent.putExtra("name", currentSelectedAppName);
        context.startService(intent);


    }

    @Override
    public void handleData() {

    }

    @Override
    public void handleDraw(Canvas canvas) {

    }

    @Override
    public void viewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_fl:
                activity.onBackPressed();
                break;
        }
    }
}
